#ifndef _PARALLELSOCKETREACTOR_HPP_
#define _PARALLELSOCKETREACTOR_HPP_

#include "jhdefs.hpp"
#include "utils/shareptr.hpp"
using JHCPP::utils::TSharedPtr;
#include "thread/thread.hpp"
using JHCPP::thread::CThread;
#include "stdlib/timespan.hpp"
using JHCPP::stdlib::CTimeSpan;

NET_NAMESPACE_BEGIN

template <class T>
class TParallelSocketReactor: public T
{
public:
	typedef TSharedPtr<TParallelSocketReactor> Ptr;

	TParallelSocketReactor()
	{
		m_thread.start(*this);
	}

	TParallelSocketReactor(const CTimeSpan& timeout) : T(timeout)
	{
		m_thread.start(*this);
	}

	~TParallelSocketReactor()
	{
		try
		{
			this->stop();
			m_thread.join();
		}
		catch (...)
		{
			jh_unexpected();
		}
	}

protected:
	void onIdle()
	{
		T::onIdle();
		CThread::yield();
	}

private:
	CThread m_thread;
};


NET_NAMESPACE_END

#endif
